package ai;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class AggregateStatistics {
	private List<Double> times1,times2;
	private int victory1;
	private int victory2;
	private int draws;
	
	public AggregateStatistics()
	{
		times1 = new ArrayList<Double>();
		times2 = new ArrayList<Double>();
		victory1 = 0;
		victory2 = 0;
	}
	
	public void registerGameStatistics(GameStatistics s)
	{
		times1.addAll(s.getTime1());
		times2.addAll(s.getTime2());
		switch(s.getGameState())
		{
		case DRAW:
			draws++;
			break;
		case PLAYING:
			break;
		case VICTORY_1:
			victory1++;
			break;
		case VICTORY_2:
			victory2++;
			break;
		
		}
	}
	
	public String toString()
	{
		DecimalFormat df = new DecimalFormat("#####.######");
		String nl = System.getProperty("line.separator");
		StringBuilder sb = new StringBuilder();
		sb.append("MeanTime1: "+ nl + df.format(mean(times1)) + nl);
		sb.append("MeanTime2: "+ nl + df.format(mean(times2)) + nl);
		List<Double> times = new ArrayList<Double>(times1);
		times.addAll(times2);
		sb.append("MeanTimeTot: "+ nl + df.format(mean(times)) + nl);
		sb.append("MaxTime1: "+ nl + df.format(max(times1)) + nl);
		sb.append("MaxTime2: "+ nl + df.format(max(times2)) + nl);
		sb.append("MaxTimeTot: "+ nl + df.format(max(times)) + nl);
		sb.append("Victory1 num: "+ nl + victory1 + nl);
		sb.append("Victory2 num: "+ nl + victory2 + nl);
		sb.append("Draws num: "+ nl + draws + nl);
		
		return sb.toString();
	}
	
	private double mean(List<Double> values)
	{
		double mean=0;
		for (Double d : values) {
			mean+=d;
		}
		return mean/((double)values.size());
	}
	
	private double max(List<Double> values)
	{
		double max=0;
		for (Double d : values) {
			if(d>max)
				max = d;
		}
		return max;
	}
}
